import 'dart:async';

import 'package:flutter_axrjc/datasource/db/DBLightApi.dart';
import 'package:flutter_axrjc/datasource/network/api/LightApi.dart';
import 'package:flutter_axrjc/datasource/network/api/UserApi.dart';
import 'package:flutter_axrjc/datasource/network/bean/EventBean.dart';
import 'package:flutter_axrjc/datasource/network/bean/LightBean.dart';
import 'package:flutter_axrjc/datasource/network/request/exp.dart';
import 'package:flutter_axrjc/main.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import 'state.dart';

class MyLightLogic extends GetxController {
  final MyLightState state = MyLightState();
  StreamSubscription<EventBean>? listener;

  var page = 1;

  var refreshController = RefreshController();
  @override
  void onInit() {
    super.onInit();
    listener = eventBus.on<EventBean>().listen((event) {
      if (event.type == EventType.refreshMyLights) {
        refreshMyLights(isRefresh: true);
      }
    });
    UserApi.getUser().then((value) {
      if (value != null) {
        state.isNotLogin.value = false;
        DBApi.getAll().then((value) {
          if (value.isNotEmpty) {
            state.list.addAll(value
                .map((e) => LightBean(
                      lastDetectionTime: e.lastDetectionTime,
                      fixtureId: e.fixtureId,
                      fixtureName: e.fixtureName,
                      fixtureBrand: e.fixtureBrand,
                      fixtureModel: e.fixtureModel,
                      fixtureImg: e.fixtureImg,
                      fixtureType: e.fixtureType,
                      installationDate: e.installationDate,
                      fixtureStatus: e.fixtureStatus,
                      brightnessAdjustment: e.brightnessAdjustment,
                      colorTemperatureAdjustment: e.colorTemperatureAdjustment,
                      remark: e.remark,
                      ifBack: e.ifBack,
                      customerId: e.customerId,
                    ))
                .toList());
          } else {
            refreshController.requestRefresh();
          }
        });
      } else {
        state.isNotLogin.value = true;
      }
    });
  }

  @override
  void onClose() {
    super.onClose();
    listener?.cancel();
  }

  Future<void> refreshMyLights({required bool isRefresh}) async {
    if (isRefresh) {
      page = 1;
      state.list.clear();
    } else {
      page++;
    }
    try {
      state.list.addAll(await LightApi.getAll(page: page));
    } catch (e) {
      if (e is ApiException) {
        EasyLoading.showToast(e.message);
      }
    }
    if (isRefresh) {
      refreshController.refreshCompleted();
    } else {
      refreshController.loadComplete();
    }
  }

  Future<void> deleteLight(String s) async {
    await LightApi.deleteLight(s);
    refreshMyLights(isRefresh: true);
  }

  void newCheck(LightBean bean) {
    eventBus.fire(EventBean(type: EventType.changeMainPage, data: 0));
    eventBus.fire(EventBean(type: EventType.newCheck, data: bean));
  }
}
